<?php

namespace App\Http\Middleware;

use Closure;
use App\Permission;

class LogUri
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $permission = Permission::firstOrNew(['name'=>$request->route()->getName()]);
        if(!$permission->id){
            $permission->display_name = $permission->name;
            $permission->description = $permission->name;
            $permission->save();
        }

        return $next($request);
    }
}